<template>
  <div class="top">
    <div
      v-for="tab in tabs"
      :key="tab.id"
      class="item"
      :class="{ check: selectedTab.id === tab.id }"
      @click="emit('change', tab)"
    >
      {{ tab.title }}
    </div>
    <div class="date-box">
      <p>{{ lang.tabtime_time }}</p>
      <div class="item time-item">
        <div class="left" @click="openStartDatePicker">
          {{ formatDate(props.startDate) }}
        </div>
        <div class="center">---</div>
        <div class="right" @click="openEndDatePicker">
          {{ formatDate(props.endDate) }}
        </div>
      </div>
    </div>
  </div>
  <!-- 日期选择器 -->
  <van-action-sheet
    v-model:show="showDatePicker"
    class="timer-piker-sheet"
    teleport="body"
    destroyOnClose
  >
    <van-date-picker
      v-model="currentDate"
      title="选择日期"
      option-height="1rem"
      @cancel="closeDatePicker"
      @confirm="changeDate"
    />
  </van-action-sheet>
</template>

<script setup lang="ts">
const lang: any = getCurrentPageLang("activity/agente");

const props = defineProps(["selectedTab", "tabs", "startDate", "endDate"]);
const emit = defineEmits(["change", "changeDate"]);

const showDatePicker = ref(false);
const pickerType = ref("start");
const currentDate = ref(new Date());

const openStartDatePicker = () => {
  pickerType.value = "start";
  currentDate.value = props.startDate;
  showDatePicker.value = true;
};
const openEndDatePicker = () => {
  pickerType.value = "end";
  currentDate.value = props.endDate;
  showDatePicker.value = true;
};
const closeDatePicker = () => {
  showDatePicker.value = false;
};
const changeDate = () => {
  emit("changeDate", currentDate.value, pickerType.value);
  closeDatePicker();
};
</script>

<style scoped lang="scss">
.top {
  width: 100%;
  display: flex;
  gap: 0.2rem;
  flex-wrap: wrap;
  box-sizing: border-box;
  margin-top: 0.2rem;

  .item {
    border-radius: var(--border-radius);
    background: var(--theme-color4);
    display: flex;
    justify-content: center;
    align-items: center;
    color: #ffffff6d;
    font-size: 0.32rem;
    min-width: 1.85rem;
    height: 0.75rem;
    cursor: pointer;
    padding: 0 0.3rem;
    transition: all 0.2s;

    &:hover {
      filter: brightness(1.05);
    }

    &.check {
      background: var(--theme-color1);
      color: var(--theme-neutral1);
    }
  }
}

.date-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.2rem;

  p {
    font-size: 0.3rem;
    color: var(--theme-neutral1);
  }

  .item {
    width: calc(100% - 1.4rem);
    height: 0.9rem;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    border: 0.01rem solid var(--theme-color1);
    div {
      display: flex;
      justify-content: center;
      height: 100%;
      align-items: center;
    }
    .left,
    .right {
      flex: 1;
    }
    .center {
      width: 1rem;
    }
  }
}
</style>
